// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-04-05 00:33
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.DynamicPlan;

import java.util.Arrays;

public class MinDistance {
    /**
     * 获取最长公共字串:结果 -> Min(n,m) - LCS + 1
     */
    public int minDistance(String word1, String word2) {
        String s1 = " " + word1, s2 = " " + word2;
        int n = word1.length(), m = word2.length(), LCS = 0;
        char[] cs1 = s1.toCharArray();
        char[] cs2 = s2.toCharArray();
        int[][] f = new int[n + 1][m + 1];
        for (int i = 0; i <= n; i++) {
            Arrays.fill(f[i], 1);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (cs1[i] == cs2[j]) {
                    f[i][j] = f[i - 1][j - 1] + 1;
                } else {
                    f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
                }
            }
        }

        LCS = f[n][m] - 1;
        return n + m - 2 * LCS;
    }
}
